
//验证码点击刷新
$(function(){
    var img = $("#img");
    var src = img.attr("src")+"?";
    img.click(function(){
        img.attr("src",src+Math.random());
    });
     var startX=0;
     var slider=document.getElementById('slider');
    var body=document.body;
    slider.addEventListener('touchstart',function(e){
            startX=e.touches[0].clientX;
        });
    slider.addEventListener('touchmove',function(e){
            var moveX=e.touches[0].clientX;
            var distanceX=moveX-startX;
              if (distanceX<-50){
                  var slider=document.getElementById('slider');
                  slider.style.left='-60%';
              }});

    body.addEventListener('touchstart',function(e){
        startX=e.touches[0].clientX;
    });
    body.addEventListener('touchmove',function(e){
        var moveX=e.touches[0].clientX;
        var distanceX=moveX-startX;
        if (distanceX>50){
            var slider=document.getElementById('slider');
            slider.style.left='0';
        }
        if (distanceX<-50){
            var slider=document.getElementById('slider');
            slider.style.left='-60%';
        }
    });


});

function getstuid(i) {
    var reg=/^[0-9]{12}$/;   /*定义验证表达式*/
    var result=reg.test(i.value);     /*进行验证*/
    if (!result){
        alert('学号为12位数');
        i.value='';
        i.focus();
    }
    i.value=i.value;
}

function getname(i) {
    var reg=/^([\u4e00-\u9fa5]){2,7}$/;   /*定义验证表达式*/
    var result=reg.test(i.value);     /*进行验证*/
    if (!result){
        alert('姓名为2～7个中文字符');
        i.value='';
        i.focus();
    }
}

function getpassword(i) {
    var reg=/^\w{6,16}$/;   /*定义验证表达式*/
    var result=reg.test(i.value);     /*进行验证*/
    if (!result){
        alert('密码长度6~16位，只允许英文字母，数字，下划线');
        i.value='';
        i.focus();
    }
}

function getpassword2(i) {
    var psw=document.getElementById('psw').value;
    if (psw!==i.value){
        alert('两次密码不一致');
        i.value='';
        i.focus();
    }
}


function getphone(i) {
    var reg=/^[1][3,4,5,7,8][0-9]{9}$/;   /*定义验证表达式*/
    var result=reg.test(i.value);     /*进行验证*/
    if (!result){
        alert('手机号码错误');
        i.value='';
        i.focus();
    }
}

var apartment;
var depar;
function getgrade(uid) {
    apartment=uid;
    $('#class').html('');
    depar=$("#cho option:selected").text();
    $("#apartment").val(depar);
}

function getclass(grade){
    $.post('getclass',{
            grade:grade,
            uid:apartment
        },
        function(data){
            $('#class').html('');
            $('#class').append(data);
        });
}


function register() {
    $.post('registeraction',{
            type:$("input[name='type']").val(),
            stuid:$("input[name='stuid']").val(),
            name:$("input[name='name']").val(),
            psw:$("input[name='psw']").val(),
            email:$("input[name='email']").val(),
            phone:$("input[name='phone']").val(),
            apartment:$("input[name='apartment']").val(),
            grade:$("select[name='grade']").val(),
            class:$("select[name='class']").val(),
            captcha:$("input[name='captcha']").val()

        },
        function(responseText){
            if (responseText==0){
                alert('验证码错误,请重新输入');
                   //验证码点击刷新
                    var img = $("#img");
                    var src = img.attr("src")+"?";
                    img.attr("src",src+Math.random());

            }else if (responseText==1){
                alert('该用户已注册');
            }else if (responseText==2){
                alert('注册成功');
                location.href="index";
            }

        });
}
function login() {
    $.post('loginaction',{
            type:$("input[name='type']").val(),
            stuid:$("input[name='stuid']").val(),
            phone:$("input[name='phone']").val(),
            psw:$("input[name='psw']").val(),
            captcha:$("input[name='captcha']").val(),
        },
        function(responseText){
            if (responseText==0){
                alert('验证码错误,请重新输入');
                //验证码点击刷新
                var img = $("#img");
                var src = img.attr("src")+"?";
                img.attr("src",src+Math.random());
            }else if (responseText==1){
                alert('该用户不存在');
            }else if (responseText==2){
                alert('登陆成功');
                location.href="index";
            }else if (responseText==3){
                alert('账号或密码错误');
            }

        });
}

function find() {
    $.post('findaction',{
            type:$("input[name='type']").val(),
            name:$("input[name='name']").val(),
            stuid:$("input[name='stuid']").val(),
            email:$("input[name='email']").val(),
            phone:$("input[name='phone']").val(),
            captcha:$("input[name='captcha']").val()

        },
        function(responseText){
            if (responseText==0){
                alert('验证码错误,请重新输入');
                //验证码点击刷新
                var img = $("#img");
                var src = img.attr("src")+"?";
                img.attr("src",src+Math.random());
            }else if (responseText==1){
                alert('信息验证正确');
                location.href="update"
            }else if (responseText==2){
                alert('验证信息错误');
            }

        });
}


function update() {
    $.post('updateaction',{
            psw:$("input[name='psw']").val(),
        },
        function(responseText){
            if (responseText==1){
                alert('密码修改成功');
                location.href="index"
            }
        });
}

function con(i,id) {
    $.post('con',{
        t:i
        },
        function(responseText){
            if (responseText==0){
                alert('请先登陆');
                location.href="login?type=1"
            }else if (responseText==1){
                location.href="confirm?t="+i+"&id="+id;
            }
        });
}

function mconfirm() {
    $.post('confirmaction',{
            t:$("input[name='t']").val(),
            uid:$("input[name='uid']").val(),
            preject:$("input[name='preject']").val(),
            name:$("input[name='name']").val(),
            stuid:$("input[name='stuid']").val(),
            apartment:$("input[name='apartment']").val(),
            grade:$("input[name='grade']").val(),
            class:$("input[name='class']").val(),
            email:$("input[name='email']").val(),
            phone:$("input[name='phone']").val()
        },
        function(responseText){
        if (responseText==0){
            alert('请勿重复报名');
        }else if (responseText==1) {
            alert('报名成功');
            location.href="center"
        }
        });
}


function myinfo() {
    $.post('myinfo',{
            stuid:$("input[name='stuid']").val(),
            name:$("input[name='name']").val(),
            email:$("input[name='email']").val(),
            phone:$("input[name='phone']").val(),
            apartment:$("input[name='apartment']").val(),
            grade:$("input[name='grade']").val(),
            class:$("input[name='class']").val(),
        },
        function(responseText){
            if (responseText==1){
                alert('更新成功');
                location.href="center"
            }
        });
}
function slider() {
    var slider=document.getElementById('slider');
    slider.style.left='0';
}


//判断是否移动端，如果是则跳转到指定的URL地址
function browserRedirect(url) {
//只读的字符串，声明了浏览器用于 HTTP 请求的用户代理头的值
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
    if (bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        window.location.replace(url);
    }else{
        window.location.replace(url);
    }
}
